package com.baosight.sczx.zm.cm.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cm.MaterialType;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cm.domain.ZMCMMaterialBase;
import com.baosight.sczx.zm.cm.domain.ZMCMMaterialBasicInfo;
import com.baosight.sczx.zm.cm.domain.ZMCMMaterialType;
import com.baosight.sczx.zm.cm.domain.ZMCMTeammaterialplanDetail;


/**材料使用管理--材料基础信息
 * <p>Title: ServiceZMCM02.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 杨鸣
 * @version 1.0
 * <pre>Histroy:
 *          2011-5-30 上午09:49:43  杨鸣  Create
 *</pre>
 */

public class ServiceZMCM02 extends ServiceEPBase {

	/**初始数据
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30 上午09:49:43  杨鸣
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.units",false));
		return outInfo;
	}

	/**列表查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30 上午09:49:43  杨鸣
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {

		//通过treeTypeInnerCode和fdMaterialCode建立和材料树的关系
		String treeTypeInnerCode = inInfo.getString("treeTypeInnerCode");//树节点ID
//		if(treeTypeInnerCode != null && !treeTypeInnerCode.equals("root")){
//			StaticTools.setQueryParameters(inInfo, "treeTypeInnerCode", treeTypeInnerCode, true);//添加查询条件
//		}

		if(treeTypeInnerCode != null){
		StaticTools.setQueryParameters(inInfo, "treeTypeInnerCode", treeTypeInnerCode, true);//添加查询条件
	    }
		
		EiInfo outInfo = super.query(inInfo, "ZMCMMaterialBasicInfo.query",new ZMCMMaterialBasicInfo());
		//此处需要修改
		//outInfo.addBlock(getIsAddTypeInfo(treeTypeInnerCode));
		return outInfo;
	}

	/**
	 * 
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-1下午08:07:00 杨鸣
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验同一类别下没有重复的 ，且物资编码没有重复
		Map<String, String> params = new HashMap<String, String>();
		String fdMaterialTypeId = inInfo.getBlock("result").getCellStr(0, "fdMaterialTypeId");	
		String fdMaterialLastCode = inInfo.getBlock("result").getCellStr(0, "fdMaterialLastCode");			
		String fdMaterialTypeInnerCode = inInfo.getBlock("result").getCellStr(0, "fdMaterialTypeInnerCode");		
		params.put("fdMaterialTypeId", fdMaterialTypeId);
		params.put("fdMaterialLastCode", fdMaterialLastCode);
		params.put("fdMaterialInnerCode", fdMaterialTypeInnerCode+fdMaterialLastCode);//保证物资编号唯一
		boolean isRepeat = validate(inInfo, "ZMCMMaterialBasicInfo.validate", params);
		
		if(isRepeat) {
			return inInfo;
		}
		else {
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CM_MATERIALBASICINFO"));//主键
			//为构造物资编码，去掉类别内码中root部分
			String noRootTypeInnerCode = fdMaterialTypeInnerCode;
			if (fdMaterialTypeInnerCode.startsWith("root")){
				noRootTypeInnerCode = fdMaterialTypeInnerCode.substring(4,fdMaterialTypeInnerCode.length());				
			}
			inInfo.getBlock("result").setCell(0, "fdMaterialCode",noRootTypeInnerCode + fdMaterialLastCode );//物资编码=分类内码(去掉root)+物资尾码
			inInfo.getBlock("result").setCell(0, "fdMaterialInnerCode",fdMaterialTypeInnerCode + fdMaterialLastCode );//物资内码=分类内码+物资尾码
			
			EiInfo outInfo = super.insert(inInfo, "ZMCMMaterialBasicInfo.insert");
			return outInfo;
		}
	}

	/**
	 * 
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-1下午08:06:26 杨鸣
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		//校验编号或名称是否重复
		Map<String, String> params = new HashMap<String, String>();
		String fdKeyId = inInfo.getBlock("result").getCellStr(0, "fdKeyId");
		String frontPrice = inInfo.getBlock("result").getCellStr(0, "fdPrice");
		params.put("fdKeyId", fdKeyId);
		
		//校验序号、编号、名称是否重复
		boolean isRepeat = validate(inInfo, "ZMCMMaterialBasicInfo.validate", params);
		if(isRepeat) {
			return inInfo;
		}
		else {
			StaticTools.setQueryParameters(inInfo, "fdKeyId", fdKeyId, true);
			EiInfo priceInfo = super.query(inInfo, "ZMCMMaterialBasicInfo.query",new ZMCMMaterialBasicInfo());
			String backPrice = priceInfo.getBlock("result").getCellStr(0, "fdPrice");
			
			String prompt = "";
			if(Double.compare(Double.parseDouble(frontPrice), Double.parseDouble(backPrice)) != 0){
				inInfo.getBlock("result").setCell(0, "fdMaterialId", fdKeyId);
				inInfo.getBlock("result").setCell(0, "fdPrice", Double.parseDouble(frontPrice));
				super.update(inInfo, "ZMCMMaterialBase.updateAfterModifyPrice");
				prompt = "温馨提示：若库存中有该物资的信息，修改单价后，会将库存中该物资的单价、库存金额进行相应的调整！";
			}
			
			EiInfo outInfo = super.update(inInfo, "ZMCMMaterialBasicInfo.update");
			if(!"".equals(prompt)){
				//outInfo.setStatus(1);
				outInfo.setDetailMsg(prompt);
			}
			return outInfo;
		}
	}
	
	/**
	 * 激活
	 * <p>Function: modifyMaterialStart</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-1下午08:07:25 杨鸣
	 * </p>
	 *
	 */
	public EiInfo modifyMaterialStart(EiInfo inInfo)
	{
//		String fdMaterialStatus = inInfo.getBlock("result").getCellStr(0, "fdMaterialStatus");
//		String sqlName = "ZMCMMaterialBasicInfo.modifyUse";
//		if(fdMaterialStatus.equals("在用")){
//			sqlName = "ZMCMMaterialBasicInfo.modifyUnUse";
//		}else{
//			sqlName = "ZMCMMaterialBasicInfo.modifyUse";
//		}
		EiInfo _temp = super.update(inInfo, "ZMCMMaterialBasicInfo.modifyUse");
		EiInfo outInfo = this.query(inInfo);
       
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 作废
	 * <p>Function: modifyMaterialStop</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-1下午08:07:25 杨鸣
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public EiInfo modifyMaterialStop(EiInfo inInfo)
	{
//		String fdMaterialStatus = inInfo.getBlock("result").getCellStr(0, "fdMaterialStatus");
//		String sqlName = "ZMCMMaterialBasicInfo.modifyUse";
//		if(fdMaterialStatus.equals("在用")){
//			sqlName = "ZMCMMaterialBasicInfo.modifyUnUse";
//		}else{
//			sqlName = "ZMCMMaterialBasicInfo.modifyUse";
//		}
		List fdKeyIds = new ArrayList();
		for(int i = 0; i < inInfo.getBlock("result").getRowCount(); i++){
			fdKeyIds.add(inInfo.getBlock("result").getCellStr(i, "fdKeyId"));
		}
		
		StaticTools.setQueryParameters(inInfo, "fdMaterialIds", fdKeyIds, true);
		EiInfo MBInfo = super.query(inInfo, "ZMCMMaterialBase.hasMaterialBase", new ZMCMMaterialBase());
		EiInfo TPInfo = super.query(inInfo, "ZMCMTeammaterialplanDetail.HasPlan", new ZMCMTeammaterialplanDetail());
		if(TPInfo.getBlock("result").getRowCount() > 0){
			TPInfo.setStatus(-1);
			StringBuffer buffer = new StringBuffer();
			buffer.append("物资资料");
			buffer.append(TPInfo.getBlock("result").getCellStr(0, "fdMaterialCode"));
			buffer.append("已经列入需求计划，不允许作废该物资资料！");
			TPInfo.setMsg(buffer.toString());
			return TPInfo;
		}else if(MBInfo.getBlock("result").getRowCount() > 0){
			MBInfo.setStatus(-1);
			StringBuffer buffer = new StringBuffer();
			buffer.append("物资资料");
			buffer.append(MBInfo.getBlock("result").getCellStr(0, "fdMaterialCode"));
			buffer.append("在库存中有该物资资料的信息，不允许作废该物资资料！");
			MBInfo.setMsg(buffer.toString());
			return MBInfo;
		}
		
		EiInfo _temp = super.update(inInfo, "ZMCMMaterialBasicInfo.modifyUnUse");
		EiInfo outInfo = this.query(inInfo);
       
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}	
	
	/**
	 * 同一节点下的已存在的物资尾码重复验证
	 * <p>Function: validateNo</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-13下午08:13:00 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdMaterialTypeId = inInfo.getBlock("result").getCellStr(0, "fdMaterialTypeId");
		String fdMaterialLastCode = inInfo.getBlock("result").getCellStr(0, "fdMaterialLastCode");
		String fdKeyId = inInfo.getBlock("result").getCellStr(0, "fdKeyId");

		params.put("fdMaterialTypeId", fdMaterialTypeId);
		params.put("fdMaterialLastCode", fdMaterialLastCode);
		params.put("fdKeyId", fdKeyId);
		
		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCMMaterialBasicInfo _materialBasicInfo = (ZMCMMaterialBasicInfo)result.get(0);
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_materialBasicInfo.getFdMaterialTypeId() != null && _materialBasicInfo.getFdMaterialTypeId().toString().equals(fdMaterialTypeId)) {
				msg.append(" 物资分类 ");
			}
			if(_materialBasicInfo.getFdMaterialLastCode() != null && _materialBasicInfo.getFdMaterialLastCode().equals(fdMaterialLastCode)) {
				msg.append(" 物资尾码 ");
			}
			msg.append("重复！");
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
	
	/**
	 * 
	 * <p>Function: getIsAddTypeInfo</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-11-24上午01:37:30 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unused", "rawtypes" })
	private EiBlock getIsAddTypeInfo(String TreeTypeInnerCode)
	{
        EiInfo info = new EiInfo();
		EiBlock block = info.addBlock("cmes.cm.isAddTypeInfo");

		EiColumn eiColumn = new EiColumn("fdKeyId");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdTypeCode");
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		eiColumn = new EiColumn("fdTypeInnerCode");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdTypeName");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdMaterialClassify");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdTypeLevel");
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		eiColumn = new EiColumn("fdIsAddmaterial");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		List<Map> list = MaterialType.getIsAddTypeInfo(TreeTypeInnerCode);
		block.setRows(list);
		return block;
	}
	/**
	 * 
	 * <p>Function: 查询物资资料</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-1 16:57:17 李红星
	 * </p>
	 *
	 */
	public EiInfo getMeterial(EiInfo inInfo) {
		EiInfo outInfo = super.query(inInfo, "ZMCMMaterialBasicInfo.queryForMaterialsCustomView",new ZMCMMaterialBasicInfo());
		return outInfo;
	}
	
	/**物资分类查询
	 * <p>Function: type</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-7下午05:35:57 贾雪民
	 * </p>
	 *
	 */
	public EiInfo type(EiInfo info) 
	{
		info = super.query(info, "ZMCMMaterialType.queryallType", new ZMCMMaterialType());
		return info;
	}
	
}
